const themeModel = require('../models/theme.model')
const errorTypes = require('../contants/error-types')

class ThemeMiddleware {

  async handleTheme(ctx, next) {

    console.log('ctx.request.query:---', ctx.request.query);

    const type = ctx.request.query.type

    const isExistedTheme = await themeModel.checkTheme(type)

    // 处理 theme 不存在的情况
    if (!isExistedTheme) {
      const error = new Error(errorTypes.THEME_DOES_NOT_EXIST)
      return ctx.app.emit('error', error, ctx)
    }

    await next()

  }

}

module.exports = new ThemeMiddleware()